﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;

namespace WXYW
{
    public partial class MyApplication : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMyApplication();
            }
        }

        /// <summary>
        /// 朱经理(1049776210)  4:25:19 PM
        /// 客户端
        /// 击查看显示详细信息例如：同时右上方加一个按钮《收货确认》
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void viewLinkButton_click(object sender, EventArgs e)
        {
            string applicationid = ((LinkButton)sender).CommandArgument;
            ApplicationService service = new ApplicationService();
            ApplicationForm form = service.GetApplicationFormByID(long.Parse(applicationid));
            //if (form.Status.Equals("已发货待客户确认"))
            //{
            //    Response.Redirect(string.Format("GiveFeedback.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, applicationid));
            //}
            //else
            //{
            if (form.ApplicationType.StartsWith("B") )
            {
                Response.Redirect(string.Format("ApplicationDetailBJXX.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, applicationid));
            }
            else
            {
                Response.Redirect(string.Format("ApplicationDetail_Client.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, applicationid));
            }
            //}
        }

        protected void updateLinkButton_click(object sender, EventArgs e)
        {
            string applicationId = ((LinkButton)sender).CommandArgument;
            ApplicationService service = new ApplicationService();
            ApplicationForm form = service.GetApplicationFormByID(long.Parse(applicationId));
            string url = string.Empty;
            switch (form.ApplicationType)
            {
                case "R":
                    url += "FillRMA.aspx";
                    break;
                case "D":
                    url += "FillDOA.aspx";
                    break;
                case "B":
                    url += "FillBJXX.aspx";
                    break;
            }
            Response.Redirect(string.Format(url+"?{0}={1}",Constants.QUERYSTR_APPLICATION_ID,applicationId));
        }

        private void BindMyApplication()
        {
            ApplicationService service = new ApplicationService();

            List<ApplicationForm> forms = service.GetApplicationFormsByUserID(ContextService.CurrentAccount.UserID);
            myApplicationGridView.DataSource = forms;
            myApplicationGridView.DataBind();
            userLabel.Text = ContextService.CurrentAccount.UserName;
            RecordsNumber = forms.Count;
        }

        protected void myApplicationGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < myApplicationGridView.PageCount)
            {
                myApplicationGridView.PageIndex = e.NewPageIndex;
            }
            
            BindMyApplication();
        }

        protected void myApplicationGridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void myApplicationGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();

                ApplicationForm application = (ApplicationForm)e.Row.DataItem;
                if (application != null && !string.IsNullOrEmpty(application.Status) &&
                    !application.Status.Equals("等待RMA单号确认") &&
                    !application.Status.Equals("待受理") &&
                    !application.Status.Equals("等待DOA单号确认") &&
                    !application.Status.Equals("备件申请-待受理"))
                {
                    ((LinkButton)e.Row.FindControl("updateLinkButton")).Enabled = false;
                }
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= myApplicationGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (myApplicationGridView.PageIndex + 1).ToString();
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<ApplicationForm> dds = new List<ApplicationForm>();
            dds.Add(new ApplicationForm());
            return dds;
        }


        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            myApplicationGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindMyApplication();
        }

        protected void myApplicationGridView_DataBound(object sender, EventArgs e)
        {
            if (myApplicationGridView.BottomPagerRow != null)
            {
                myApplicationGridView.BottomPagerRow.Visible = true;
            }
        }
    }
}
